<template>
  <div class = "addPicturePage">
    <h2 style = "margin-bottom: 16px ">
       {{route.query?.id ? "修改" : "创建"}}图片
    </h2>

    <a-typography-paragraph v-if="spaceId" type="secondary">
      保存至空间：<a :href="`/space/${spaceId}`" target="_blank">{{ spaceId }}</a>
    </a-typography-paragraph>

    <!-- 选择上传方式 -->
    <a-tabs v-model:activeKey="uploadType"
    >>
      <a-tab-pane key="file" tab="文件上传">
        <PictureUpload :picture="picture" :spaceId="spaceId" :onSuccess="onSuccess" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL 上传" force-render>
        <UrlPictureUpload :picture="picture" :spaceId="spaceId" :onSuccess="onSuccess" />
      </a-tab-pane>
    </a-tabs>

    <!-- 图片上传表单 -->
    <a-form v-if="picture" name = "pictureForm" layout="vertical" :model="pictureForm" @finish="handleSubmit">
      <a-form-item name = "name" label="名称">
        <a-input v-model:value="pictureForm.name" placeholder="输入图片名称" allow-clear />
      </a-form-item>
      <a-form-item name = "introduction" label="简介">
        <a-textarea
          v-model:value="pictureForm.introduction" placeholder="输入图片简介" allow-clear />
      </a-form-item>
      <a-form-item name = "category" label="分类">
        <a-auto-complete v-model:value="pictureForm.category" placeholder="输入图片分类" :options = "categoryOptions" allow-clear/>
      </a-form-item>
      <a-form-item name = "tags" label="标签">
        <a-select  v-model:value="pictureForm.tags" mode = "tags" placeholder="输入图片标签" :options = "tagsOptions" allow-clear />
      </a-form-item>
      <a-form-item  label="创建">
        <a-button type = "primary" html-type="submit" style = "width: 100%">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive, onMounted, computed} from "vue"
import PictureUpload  from "@/components/PictureUpload.vue"
import {editPictureUsingPost, listPictureTagCategoryUsingGet, getPictureVoByIdUsingGet} from "@/api/pictureController.ts";
import {message} from "ant-design-vue";
import { useRouter, useRoute }  from 'vue-router'
import UrlPictureUpload from "@/components/UrlPictureUpload.vue";

const router = useRouter()
const picture = ref<API.PictureVO>()
const pictureForm = reactive<API.PictureEditRequest>({

})
const categoryOptions = ref<API.PictureTagCategory>()
const tagsOptions = ref<API.PictureTagCategory>()
const spaceId = computed(() =>{
  return route.query?.spaceId
})

const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureForm.name = newPicture.name
}

const uploadType = ref<'file' | 'url'>('file')



//创建上传图片
const handleSubmit = async (values: any) => {
  const pictureId = picture.value.id
  if(!pictureId ){
    return
  }
  const pictureUrl = picture.value?.url
  const res = await editPictureUsingPost({
    id: pictureId,
    spaceId: spaceId.value,
    url:pictureUrl,
    ...values
  })
  //操作成功
  if(res.data.code === 200 && res.data.data){
    message.success("图片创建成功")
    router.push({
      path: `/picture/${pictureId}`,
    })
  }else {
    message.error("图片创建失败")
  }
}

//获取标签分类列表
const getTagCategoryOptions = async () => {

  const res = await listPictureTagCategoryUsingGet()
  //操作成功
  if(res.data.code === 200 && res.data.data){
    tagsOptions.value =( res.data.data.tagList ?? []).map((data: string) => {
      return {
        label: data,
        value: data
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  }else {
    message.error("获取标签分类列表创建失败")
  }
}

onMounted(() => {
  getTagCategoryOptions()
})

const route = useRoute()
const getOldPicture = async () => {
  const id = route.query?.id
  if(id){
    const res = await getPictureVoByIdUsingGet({
      id
    })
    if(res.data.code === 200 && res.data.data){
      const data = res.data.data
      picture.value = data
      pictureForm.name = data.name
      pictureForm.introduction = data.introduction
      pictureForm.category = data.category
      pictureForm.tags = data.tags
    }
  }
}

onMounted(() => {
  getOldPicture()
})


</script>

<style scoped>
.addPicturePage{
  max-width: 720px;
  margin: 0 auto;

}
</style>
